package com.weixing.mall.provider.controller.auth;

import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.*;
import com.weixing.mall.provider.api.model.enums.AgentRoleEnum;
import com.weixing.mall.provider.api.model.vo.UserInfoVo;
import com.weixing.mall.provider.api.model.vo.VueAuthorizeBean;
import com.weixing.mall.provider.model.domain.User;
import com.weixing.mall.provider.model.enums.sys.SysFlag;
import com.weixing.mall.provider.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 认证接口
 *
 * @author  kfc
 * @since  1.0
 */
@RestController
@Validated
public class PassportController extends BaseController {

    @Autowired
    private IUserService sysUserService;


    /**
     * 切换头像
     *
     * @param avatar  头像
     * @return  修改结果
     */
    @PostMapping("changeAvatar")
    public Result changeAvatar(@NotNull(message = "头像不能为空")
                                     @RequestParam String avatar){
        SysAuth auth = AuthUtil.getSysExt();
        sysUserService.changeAvatar(avatar,auth.getUid());
        return ResultUtil.success();
    }


    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("userInfo")
    public Result<UserInfoVo> info(){
        SysAuth auth = AuthUtil.getSysExt();
        // vue权限相关
        List<VueAuthorizeBean> vueAuthorizeBeans = sysUserService.loadVueAuthorize(auth.getUid(), SysFlag.SYS);
        User sysUser = sysUserService.getById(auth.getUid());
        UserInfoVo infoVo = new UserInfoVo();
        infoVo.setAvator(sysUser.getAvator());
        infoVo.setUserName(sysUser.getLoginName());
        infoVo.setVuePaths(vueAuthorizeBeans);
        return ResultUtil.success(infoVo);
    }


    /**
     * 代理商登录详细信息
     *
     * @return  代理登录详细信息
     */
    @GetMapping("agentInfo")
    public Result<UserInfoVo> agentInfo(){
        AgentAuth agentAuth = AuthUtil.getAgentExt();
        Long agentId = agentAuth.getAid();
        AgentRoleEnum role = AgentRoleEnum.getRole(agentAuth.getAlevel(), agentAuth.getAType());
        String roleCode = role.getValue();
        // vue权限相关
        List<VueAuthorizeBean> vueAuthorizeBeans = sysUserService.loadVueAuthorizeByRoleCode(roleCode, SysFlag.AGENT);
        UserInfoVo infoVo = new UserInfoVo();
        infoVo.setVuePaths(vueAuthorizeBeans);
        return ResultUtil.success(infoVo);
    }


}
